package com.niyue.sandbox.uclock.clockr;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.xml.sax.SAXException;

import com.aetrion.flickr.Flickr;
import com.aetrion.flickr.FlickrException;
import com.aetrion.flickr.groups.pools.PoolsInterface;
import com.aetrion.flickr.photos.Photo;
import com.aetrion.flickr.photos.PhotosInterface;
import com.aetrion.flickr.tags.Tag;

public class Clockr implements FlickrDigitPool {
	private String[][] tagMatrix=new String[][]{
			{"00","zero","number0"},
			{"1","one","number1"},
			{"2","tow","number2"},
			{"3","three","number3"},
			{"4","four","number4"},
			{"5","five","number5"},
			{"6","six","number6"},
			{"7","seven","number7"},
			{"8","eight","number8"},
			{"9","nine","number9"}			
	};
	//flickr api key of mine
	private static String API_KEY = "230a360744ebc3a78e353d00f1842eef";
	//group id of one digit group
	private static String GROUPID = "54718308@N00";
	private static String ONEDIGIT = "onedigit";
	private int maxPhoto = 100;	
	private Flickr flickr = new Flickr(API_KEY);
	private Map < Integer, List < BufferedImage > > digitMap;
	protected Map < String, Integer > tagMap;
	private int photosLoaded;
	private StatusSubscriber statusSubscriber = null;
	
	public StatusSubscriber getStatusSubscriber() {
		return statusSubscriber;
	}
	public void setStatusSubscriber(StatusSubscriber statusSubscriber) {
		this.statusSubscriber = statusSubscriber;
	}

	public Clockr(int maxPhotoNum) {
		maxPhoto=maxPhotoNum;
	}
	/*
	 * initialize the tag map
	 */
	protected void initTagMap(){
		tagMap=new HashMap<String,Integer>();
		for (int i = 0; i < tagMatrix.length; i++) {
			String[] numberTags=tagMatrix[i];
			for (int j = 0; j < numberTags.length; j++) {
				String numberTag = numberTags[j];
				tagMap.put(numberTag,i);				
			}			
		}
	}
	public void initialize(){
		initTagMap();
		photosLoaded = 0;
		digitMap=new HashMap<Integer, List<BufferedImage>>();
		PoolsInterface poolsInterface=flickr.getPoolsInterface();
		PhotosInterface photosInterface=flickr.getPhotosInterface();
		Photo photo=null;
		Photo realPhoto=null;
		Collection tags=null;
		try {
			Collection digits=poolsInterface.getPhotos(GROUPID, new String[]{ONEDIGIT}, maxPhoto, 0);
			for (Iterator iter = digits.iterator(); iter.hasNext();) {
				photo = (Photo) iter.next();
				realPhoto=photosInterface.getPhoto(photo.getId());
				String title=realPhoto.getTitle();
				//if we can infer the number from its title
				if(tagMap.containsKey(title)){
					Integer number=tagMap.get(title);
					if(!digitMap.containsKey(number)){
						digitMap.put(number, new ArrayList<BufferedImage>());
					}
					digitMap.get(number).add(realPhoto.getSmallSquareImage());
					photoLoaded();
					continue;
				}
				tags=realPhoto.getTags();
				for (Iterator iterator = tags.iterator(); iterator.hasNext();) {
					String tag = ((Tag) iterator.next()).getValue();
					//add the photo to its group by the tag
					if(tagMap.containsKey(tag)){
						Integer number=tagMap.get(tag);
						if(!digitMap.containsKey(number)){
							digitMap.put(number, new ArrayList<BufferedImage>());
						}
						digitMap.get(number).add(realPhoto.getSmallSquareImage());
						photoLoaded();
						break;
					}
				}
				// one more photo has been loaded

			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (FlickrException e) {
			e.printStackTrace();
		}
	}
	protected void photoLoaded(){
		photosLoaded++;
		if(null != statusSubscriber){
			statusSubscriber.update(photosLoaded);
		}
	}
	public BufferedImage getImgesByDigit(int digit) {
		List<BufferedImage> digits=digitMap.get(digit);
		int random=(int)Math.round(Math.random()*(digits.size()-1));
		return digits.get(random);
	}
	public Photo getPhotoById(String photoId) {
		PhotosInterface photosInterface=flickr.getPhotosInterface();
		Photo photo=null;
		try {
			photo=photosInterface.getPhoto(photoId);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (FlickrException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		return photo;
	}
	public int getMaxPhoto() {
		return maxPhoto;
	}
	public void setMaxPhoto(int maxPhoto) {
		this.maxPhoto = maxPhoto;
	}
}
